//
//  XMMineView.m
//  XMJJ_zml
//
//  Created by apple on 2025/5/9.
//

#import "XMPersonView.h"
#import "XMCityView.h"
#import "XMMineHomeCell.h"
#import "XMTopTabSelectView.h"
#import "XMPropertyView.h"
#import "XMShopViewController.h"
@interface XMPersonView ()<UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout,XMCityViewDelegate>

@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) XMPropertyView *topTabSelectView;


//@property (nonatomic, strong) XMUserInfoModel *userInfoModel;

@end

@implementation XMPersonView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self creatUI];
    }
    return self;
}

- (void)creatUI {
    UIView *titleView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, KScaleWidth(612.0))];
    [titleView setGradientLayerWithLocations:@[@(0), @(1.0)] colors:@[(__bridge id)kColorHEX(0x7A6A3F).CGColor, (__bridge id)kColorHEX(0x817A66).CGColor] startPoint:CGPointMake(0.5, 0) endPoint:CGPointMake(0.5, 1)];
    [self addSubview:titleView];
    [self addSubview:self.collectionView];
    UIView *topView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.width, kNavagationBarH)];
    topView.backgroundColor = kColorHEX(0x7A6A3F);
    [self addSubview:topView];
    
//    UIButton *addFriendBtn = [UIButton buttonWithType:UIButtonTypeCustom frame:CGRectMake(KScaleWidth(10.0), KScaleWidth(43.0), KScaleWidth(32.0), KScaleWidth(32.0)) image:@"navBack" selectImg:@"navBack"];
//    [addFriendBtn addTarget:self action:@selector(addFriendAction) forControlEvents:UIControlEventTouchUpInside];
//    [self addSubview:addFriendBtn];
    
    [self addSubview:self.topTabSelectView];
    

}

- (void)attionAction
{
    
    if (self.delegate && [self.delegate respondsToSelector:@selector(attionAction)]) {
         [self.delegate attionAction];
     }
    
}
-(void)goShop {
    if (self.delegate && [self.delegate respondsToSelector:@selector(goShop)]) {
         [self.delegate goShop];
     }
}
- (void)sendMessageAction
{
    //扫一扫
    if (self.delegate && [self.delegate respondsToSelector:@selector(sendMessageAction)]) {
         [self.delegate sendMessageAction];
     }
   
 }

#pragma mark - CollectionViewDelegate
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return 10;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
    return CGSizeMake(self.width, KScaleWidth(470.0));
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
        XMCityView *headView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"XMMineHomeHeadViewName" forIndexPath:indexPath];
//        [headView resetInfoWithUserInfoModel:_userInfoModel];
        headView.delegate = self;
        XMWeakSelf;
//        headView.homePageEditBlock = ^{
//            
//        };
        return headView;
    }
    return nil;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    CGFloat itemWidth = floor((self.width - KScaleWidth(8.0)*4)/3);
    return CGSizeMake(itemWidth, KScaleWidth(154.0));
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    XMMineHomeCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"XMMineHomeCellName" forIndexPath:indexPath];
    [cell setContentWithProduction:nil];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    [collectionView deselectItemAtIndexPath:indexPath animated:YES];
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    _topTabSelectView.y = MAX(KScaleWidth(420.0) - scrollView.contentOffset.y, kNavagationBarH);
}

#pragma mark - Init
- (UICollectionView *)collectionView {
    if (!_collectionView) {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        CGFloat itemWidth = floor((self.width - KScaleWidth(8.0)*4)/3);
        layout.itemSize = CGSizeMake(itemWidth, KScaleWidth(154.0));
        layout.minimumInteritemSpacing = KScaleWidth(8.0);
        layout.minimumLineSpacing = KScaleWidth(8.0);
        layout.sectionInset = UIEdgeInsetsMake(KScaleWidth(8.0), KScaleWidth(8.0), KScaleWidth(8.0), KScaleWidth(8.0));
        
        _collectionView = [[UICollectionView alloc] initWithFrame:self.bounds collectionViewLayout:layout];
        _collectionView.backgroundColor = kColorHEX(0xF5F5F5);
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        [_collectionView registerClass:[XMMineHomeCell class] forCellWithReuseIdentifier:@"XMMineHomeCellName"];
        [_collectionView registerClass:[XMCityView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"XMMineHomeHeadViewName"];
        _collectionView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
      
    }
    return _collectionView;
}
- (XMPropertyView *)topTabSelectView
{
    if (!_topTabSelectView) {
        _topTabSelectView = [[XMPropertyView alloc] initWithFrame:CGRectMake(0, KScaleWidth(420.0), self.width, KScaleWidth(50.0))];
    }
    return _topTabSelectView;
}
//- (XMTopTabSelectView *)topTabSelectView {
//    if (!_topTabSelectView) {
//        _topTabSelectView = [[XMTopTabSelectView alloc] initWithFrame:CGRectMake(0, KScaleWidth(600.0), self.width, KScaleWidth(50.0))];
//    }
//    return _topTabSelectView;
//}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
